public class Sort {
    public  void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= 0 ; j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }
    public void shellSort(int[] array) {
        int gap = array.length;
        while(gap > 1) {
         gap = gap/2;
         shell(array,gap);
        }
    }
    private void shell(int[] array,int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i-gap;
            for (; j >= 0; j-=gap) {
                if (array[j] > tmp) {
                    array[j+gap] = array[j];
                }else{
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }
    private void swap(int[] array,int i,int j) {
        int tmp = 0;
        tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    public void bubbleSort(int[] array) {
        //i代表比较趟数
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = true;
            for (int j = 0; j < array.length-1; j++) {
                if(array[j] > array[j+1]) {
                    swap(array,j,j+1);
                    flg = false;
                }
            }
            if (flg) {
                break;
            }
        }
    }
    //选择排序，每次选择最小的数放在i位置上，从i+1位置上开始查找
    public void selectSort(int array[]) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i+1; j < array.length; j++) {
                if(array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array,minIndex,i);
        }
    }
    public void quickSort(int[] array) {
        recursionQuick(array,0,array.length-1);
    }
    private void recursionQuick(int[] array,int start,int end) {
        if(start >= end) {
            return;
        }
        int find = quick(array,start,end);
        recursionQuick(array,0,find-1);
        recursionQuick(array,find+1,end);
    }
    //寻找基数，并进行一趟排序
    private int quick(int[] array,int start,int end) {
        int tmp = array[start];
        while(start != end ) {
            while(array[end] > tmp && start < end) {
                end--;
            }
            //走完上面的while循环后end下标遇到了小于tmp的值
            array[start] = array[end];
            while(array[start] <= tmp && start < end) {
                start++;
            }
            //此时start遇到大于tmp的数
            array[end] = array[start];
        }
        //走出循环此时start == end,把tmp（基数）放到中间
        array[start] = tmp;
        return start;
    }
    public void margeSort(int[] array) {
        recursionMarge(array,0,array.length-1);
    }
    private void recursionMarge(int[] array,int left, int right) {
        if(left >= right) {
            return;
        }
        int mid =left+(right-left)/2;//分组
        recursionMarge(array,left,mid);//分解左边
        recursionMarge(array,mid+1,right);//分解右边
        marge(array,left,right,mid);//合并
    }
    private void marge(int[] array,int left ,int right,int mid) {
        int s1 = left;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = right;
        //确定临时数组长度，用来存储合并后的数组
        int[] tmp = new int[right-left+1];
        int k = 0;
        //1.首先确保两个表都有数据
        while(s1<=e1 && s2<=e2) {
            if(array[s1] <= array[s2]) {
                tmp[k++] = array[s1++];
            }else {
                tmp[k++] = array[s2++];
            }
        }
        //2.看看那个数组还有数据，拷贝回去
        while(s1 <= e1) {
            tmp[k] = array[s1];
            k++;
            s1++;
        }
        while(s2 <= e2) {
            tmp[k] = array[s2];
            k++;
            s2++;
        }
        //3.把有序数据拷贝到原来的数组
        for (int i = 0; i < k; i++) {
            array[i+left] = tmp[i];
         }
    }
}